<?php

class NewsController extends Common {

    private $newsModel;

    public function __construct() {
        parent::__construct();
        $this->load->model('news');
        $this->newsModel = $this->model('news');
    }

    /**
     * 列表
     */
    public function listAction() {
        $userid = $this->session->get('user_id');   //从session中获取用户id
        if(empty($userid)){
            echo "请重新登录";exit;
        }
        $page     = $this->get('page') ? $this->get('page') : 1;
        $pagesize = 10;
        $pagelist = $this->instance('pagelist');
        $pagelist->loadconfig();
        $where = '';
        $model = $this->model('news');
        $total = $model->count('news', 'id', $where);
        $urlparam = array(
            'page' => '{page}',
        );
        $data = $model->page_limit($page, $pagesize)->order(array('id DESC'))/*->where($where)*/->select();
        $pagelist = $pagelist->total($total)->url(url('admin/news/list', $urlparam))->num($pagesize)->page($page)->output();
        $this->view->assign(array(
            'page' => $page,
            'data' => $data,
            'pagelist'=> $pagelist,
        )); //assign()对视图变量完成赋值
        $this->view->display('/admin/news/news_list');  //这里是对视图文件的加载
    }


    /**
     * 添加资讯
     */
    public function createAction()
    {
        $userid = $this->session->get('user_id');   //从session中获取用户id
        if ($this->post('submit')) {
            $data['title'] = $_POST['title'];     //标题
            $data['dsp'] = $_POST['dsp'];     //描述
//            $data['src'] = $_POST['src'];   //图片
            $data['url'] = $_POST['url'];   //外部链接
            $data['create_time'] = time();
            $data['update_time'] = time();
            if(empty($data['title']) /*|| empty($data['src']) */|| empty($data['url'])){
                echo "标题、图片、外部链接必填";exit;
            }

            $file = $_FILES['file'];
            //得到文件名称
            $name = time().rand(1000,9999).'.jpg';
            if(empty($file) || empty($name)){
                $this->adminMsg('上传失败,请选择图片', url('admin/news/create'),3, 1, 0);
            }
            $type = strtolower(substr($name,strrpos($name,'.')+1)); //得到文件类型，并且都转化成小写
            $allow_type = array('jpg','jpeg','gif','png'); //定义允许上传的类型
            //判断文件类型是否被允许上传
            if(!in_array($type, $allow_type)){
                //如果不被允许，则直接停止程序运行
                $this->adminMsg('上传失败,图片类型有误', url('admin/news/create'),3, 1, 0);
            }
            //判断是否是通过HTTP POST上传的
            if(!is_uploaded_file($file['tmp_name'])){
                //如果不是通过HTTP POST上传的
                $this->adminMsg('上传失败,提交类型有误', url('admin/news/create'),3, 1, 0);
            }
            $upload_path = FCPATH."/uploadfiles/image/news/"; //上传文件的存放路径
            //开始移动文件到相应的文件夹
            if(!move_uploaded_file($file['tmp_name'],$upload_path.$name)){
                $this->adminMsg('上传失败', url('admin/news/create'),3, 1, 0);
            }
            
            $model = $this->model('news');
            $data['create_time'] = $data['update_time'] = time();
            $data['src'] = $name;
            $insert_id = $model->insert($data);
            if(empty($insert_id)){
                $this->adminMsg('添加失败', url('admin/news/create'),3, 1, 0);
            }
            $this->adminMsg('添加成功', url('admin/news/create'), 3, 1, 1);
        }
        $this->view->display('/admin/news/add_news');  //这里是对视图文件的加载
    }

    /**
     * 更新资讯
     */
    public function updateAction()
    {
        $userid = $this->session->get('user_id');   //从session中获取用户id
        $id = $_REQUEST['id'];
        if(empty($id)){
            $this->adminMsg('查看失败', url('admin/news/list'),3, 1, 0);
        }
        $newsInfo = $this->newsModel->getNewById($id);
        if ($this->post('submit')) {
            $data['title'] = $_POST['title'];     //标题
            $data['dsp'] = $_POST['dsp'];     //描述
//            $data['src'] = $_POST['src'];   //图片
            $data['url'] = $_POST['url'];   //外部链接
            $data['create_time'] = time();
            $data['update_time'] = time();
            if(empty($data['title']) /*|| empty($data['src']) */|| empty($data['url'])){
                echo "标题、图片、外部链接必填";exit;
            }

            $file = $_FILES['file'];
            if(!empty($file['name'])){
                //得到文件名称
                $name = time().rand(1000,9999).'.jpg';
                $data['src'] = $name;
                if(empty($file) || empty($name)){
                    $this->adminMsg('上传失败,请选择图片', url('admin/news/list'),3, 1, 0);
                }
                $type = strtolower(substr($name,strrpos($name,'.')+1)); //得到文件类型，并且都转化成小写
                $allow_type = array('jpg','jpeg','gif','png'); //定义允许上传的类型
                //判断文件类型是否被允许上传
                if(!in_array($type, $allow_type)){
                    //如果不被允许，则直接停止程序运行
                    $this->adminMsg('上传失败,图片类型有误', url('admin/news/list'),3, 1, 0);
                }
                //判断是否是通过HTTP POST上传的
                if(!is_uploaded_file($file['tmp_name'])){
                    //如果不是通过HTTP POST上传的
                    $this->adminMsg('上传失败,提交类型有误', url('admin/news/list'),3, 1, 0);
                }
                $upload_path = FCPATH."/uploadfiles/image/news/"; //上传文件的存放路径
                //开始移动文件到相应的文件夹
                if(!move_uploaded_file($file['tmp_name'],$upload_path.$name)){
                    $this->adminMsg('上传失败', url('admin/news/list'),3, 1, 0);
                }
            }
            $model = $this->model('news');
            $data['create_time'] = $data['update_time'] = time();

            $update_id = $model->update($data,'id ='.$id);
            $this->adminMsg('更新成功', url('admin/news/list'), 3, 1, 1);
        }
        $this->view->assign(array(
            'data' => $newsInfo,
            'src'=>"http://".$_SERVER['HTTP_HOST']."/ACarAdvertistingCms/uploadfiles/image/news/".$newsInfo['src']
        )); //assign()对视图变量完成赋值
        $this->view->display('/admin/news/show_news');  //这里是对视图文件的加载
    }

    /**
     * 删除资讯
     */
    public function delAction()
    {
        $userid = $this->session->get('user_id');   //从session中获取用户id
        $id = $_REQUEST['id'];
        if(empty($id)){
            $this->adminMsg('参数缺失', url('admin/news/list'),3, 1, 0);
        }
        $model = $this->model('news');
        $update_id = $model->delete('id ='.$id);
        $this->adminMsg('删除成功', url('admin/news/list'), 3, 1, 1);

        $this->view->display('/admin/news/list');  //这里是对视图文件的加载
    }



}
